package gameheartv2;
import java.io.*;
import java.net.*;


public class UDPServer implements Runnable
{
    private Thread thread = null;

     public void run()  {
         byte[] receiveData = new byte[1024];
            byte[] sendData = new byte[1024];
         try
            {
            DatagramSocket serverSocket = new DatagramSocket(9876);
            while(true)
               {
                  DatagramPacket receivePacket = new DatagramPacket(receiveData, receiveData.length);
                  serverSocket.receive(receivePacket);
                  String sentence = new String( receivePacket.getData());
                  sentence = sentence.trim();
                 
                  System.out.println("RECEIVEDfdsf: " + sentence);
                  if(sentence.equals("1"))
                  {                      
                      InetAddress IPAddress = receivePacket.getAddress();
                      int port = receivePacket.getPort();

                      sendData = "2".getBytes();
                      DatagramPacket sendPacket =
                      new DatagramPacket(sendData, sendData.length, IPAddress, port);
                      serverSocket.send(sendPacket);
                   }
               }
         }
            catch(Exception ex)
            {

            }
     }


    public void start() {
        if (thread == null) {
            thread = new Thread(this);
            thread.start();
        }
    }

    public void stop() {
        if (thread != null) {
            thread.stop();
            thread = null;
        }
    }

//   public static void main(String args[])
//      {
//         UDPServer server = new UDPServer();
//         server.start();
//      }
}